Skip to content

Programming Paradigms

Alt text

Programming paradigm

  • A programming paradigm is a set of programming concepts.

  • We have already considered two different programming paradigms: low-level and imperative (procedural) programming.

  • The style and capability of any programming language is defined by its paradigm.

  • Some programming languages, for example JavaScript, only follow one paradigm; others, for example Python, support multiple paradigms. Most programming languages are multi-paradigm.

Low-level programming

  • Low-level programming uses instructions from the computer’s basic instruction set.
  • Assembly language and machine code both use low-level instructions.
  • This type of programming is used when the program needs to make use of specific addresses and registers in a computer, for example when writing a printer driver.

Alt text

Alt text

Imperative programming

  • In imperative programming, the steps required to execute a program are set out in the order they need to be carried out.
  • This programming paradigm is often used in the early stages of teaching programming.
  • Imperative programming is often developed into structured programming, which has a more logical structure and makes use of procedures and functions, together with local and global variables.
  • Imperative programming is also known as procedural programming.
py
#0(n)
n = int(input())
total = 0
for i in range(1,n):
    total += i
print(total)

Object-oriented programming

  • Object-oriented programming (OOP) is a programming methodology that uses self-contained objects, which contain programming statements (methods) and data, and which communicate with each other.
  • This programming paradigm is often used to solve more complex problems as it enables programmers to work with real life things.
  • Many procedural programming languages have been developed to support OOP.
  • For example, Java, Python and Visual Basic all allow programmers to use either procedural programming or OOP.

Class

  • A class is a template defining the methods and data of a certain type of object.
  • The attributes are the data items in a class.
  • A method is a programmed procedure that is defined as part of a class.
  • Putting the data and methods together as a single unit, a class, is called encapsulation.
  • To ensure that only the methods declared can be used to access the data within a class, attributes need to be declared as private and the methods need to be declared as public.
  • For example, a shape can have name, area and perimeter as attributes and the methods set shape, calculate area, calculate perimeter. This information can be shown in a class diagram.

Alt text

Object

  • When writing a program, an object needs to be declared using a class type that has already been defined.
  • An object is an instance of a class that is self- contained and includes data and methods.
  • Properties of an object are the data and methods within an object that perform named actions.
  • An occurrence of an object during the execution of a program is called an instance.

Alt text

Inheritance

  • Inheritance is the process by which the methods and data from one class, a superclass or base class, are copied to another class, a derived class.
  • Figure shows single inheritance, in which a derived class inherits from a single superclass.
  • Multiple inheritance is where a derived class inherits from more than one superclass.

Alt text

  • A base class employee and the derived classes partTime and fullTime are defined.
  • The objects permanentStaff and temporaryStaff are instanced in these examples and use the method showDetails.

Alt text

  • Figure shows the inheritance diagram for the base class employee and the derived classes partTime and fullTime.

Alt text

Polymorphism & Overloading

  • Polymorphism is when methods are redefined for derived classes.
  • Overloading is when a method is defined more than once in a class so it can be used in different situations.
  • A base class shape is defined, and the derived classes rectangle and circle are defined.
  • The method area is redefined for both the rectangle class and the circle class.
  • The objects myRectangle and myCircle are instanced in these programs.

Alt text

  • One way of overloading a method is to use the method with a different number of parameters.
  • For example, a class greeting is defined with the method hello.
  • The object myGreeting is instanced and uses this method with no parameters or one parameter in this Python program.
  • This is how Python, VB and Java manage overloading.

Alt text

Containment & aggregation

  • Containment, or aggregation, is the process by which one class can contain other classes.
  • This can be presented in a class diagram.
  • When the class ‘aeroplane’ is defined, and the definition contains references to the classes – seat, fuselage, wing, cockpit – this is an example of containment.

Constructors, setters, getters, and destructors

  • In OOP, the basic methods used during the life of an object can be divided into these types: constructors, setters, getters, and destructors.
    • A constructor is the method used to initialise a new object. Each object is initialised when a new instance is declared. When an object is initialised, memory is allocated.
    • A setter is a method used to control changes to any variable that is declared within an object. When a variable is declared as private, only the setters declared within the object’s class can be used to make changes to the variable within the object, thus making the program more robust.
    • A getter is a method that gets the value of a property of an object.
    • A destructor is a method that is invoked to destroy an object. When an object is destroyed the memory is released so that it can be reused.

Alt text

Declarative programming

  • Declarative programming is used to extract knowledge by the use of queries from a situation with known facts and rules.
  • In Chapter 8, Section 8.3 we looked at the use of SQL scripts to query relational databases.
  • It can be argued that SQL uses declarative programming.
sql
SELECT FirstName,SecondName
FROM Student
WHERE ClassID = '7A'
ORDER BY SecondName
  • Declarative programming uses statements of facts and rules together with a mechanism for setting goals in the form of a query.
  • A fact is a ‘thing’ that is known, and rules are relationships between facts.
  • Writing declarative programs is very different to writing imperative programs.
  • In imperative programming, the programmer writes a list of statements in the order that they will be performed.
  • But in declarative programming, the programmer can state the facts and rules in any order before writing the query.